﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using xbd.s7netplus;

namespace lyh.FG2JiaYaZhanDemo
{
    public  class SysInfoServer
    {
        /// <summary>
        /// 读取配置文件返回sysInfo对象
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public SysInfo GetSysInfoFromPath(string path)
        {
            try
            {
                //读取配置文件
                SysInfo ss = new SysInfo();
                ss.IPAddress = IniConfigHelper.ReadIniData("通信参数", "IP地址", "127.0.0.1", path);
                ss.CpuType = (CpuType)Enum.Parse(typeof(CpuType), IniConfigHelper.ReadIniData("通信参数", "CPU类型", "S71500", path), true);
                ss.Rack = Convert.ToInt16(IniConfigHelper.ReadIniData("通信参数", "机架号", "0", path));
                ss.Slot = Convert.ToInt16(IniConfigHelper.ReadIniData("通信参数", "插槽号", "0", path));

                ss.AutoStart = IniConfigHelper.ReadIniData("系统参数", "开机启动", "1", path) == "1";
                ss.ScreenTime = Convert.ToInt32(IniConfigHelper.ReadIniData("系统参数", "息屏时间", "0", path));
                ss.LogoffTime = Convert.ToInt32(IniConfigHelper.ReadIniData("系统参数", "注销时间", "0", path));
                ss.CameraIndex = Convert.ToInt32(IniConfigHelper.ReadIniData("系统参数", "摄像头序号", "0", path));
                return ss;
            }
            catch (Exception ex)
            {
                return null;
            }
        } 

        /// <summary>
        /// 将sysInfo对象写入配置文件
        /// </summary>
        /// <param name="path"></param>
        /// <param name="sysInfo"></param>
        /// <returns></returns>
        public bool SetSysInfoToPath(SysInfo sysInfo, string path )
        {
            bool result = true;
            result &= IniConfigHelper.WriteIniData("通信参数", "IP地址", sysInfo.IPAddress, path);
            result &= IniConfigHelper.WriteIniData("通信参数", "CPU类型", sysInfo.CpuType.ToString(), path);
            result &= IniConfigHelper.WriteIniData("通信参数", "机架号", sysInfo.Rack.ToString(), path);
            result &= IniConfigHelper.WriteIniData("通信参数", "插槽号", sysInfo.Slot.ToString(), path);

            result &= IniConfigHelper.WriteIniData("系统参数", "开机启动", sysInfo.AutoStart ? "1" : "0", path);
            result &= IniConfigHelper.WriteIniData("系统参数", "息屏时间", sysInfo.ScreenTime.ToString(), path);
            result &= IniConfigHelper.WriteIniData("系统参数", "注销时间", sysInfo.LogoffTime.ToString(), path);
            result &= IniConfigHelper.WriteIniData("系统参数", "摄像头序号", sysInfo.CameraIndex.ToString(), path);
            return result;
        }
    }
}
